package hausaufgaben;

public class LinUnab
{
  private Funktion f1, f2, f3;
  private int durchlaeufe;

  public LinUnab(Funktion f1, Funktion f2, Funktion f3)
  {
    this.f1 = f1;
    this.f2 = f2;
    this.f3 = f3;
  }

  public boolean isLinUnab(double a, double b)
  {
    double diff = b - a;
    
    for(int i = 0; i<10; i++)
    {
      double random1 = Math.random() * diff + a;
      double random2 = Math.random() * diff + a;
      double random3 = Math.random() * diff + a;
      
      double[] vektor1 = new double[3];
      vektor1[0] = f1.getFunktionswert(random1);
      vektor1[1] = f1.getFunktionswert(random2);
      vektor1[2] = f1.getFunktionswert(random3);
      
      double[] vektor2 = new double[3];
      vektor2[0] = f2.getFunktionswert(random1);
      vektor2[1] = f2.getFunktionswert(random2);
      vektor2[2] = f2.getFunktionswert(random3);
      
      double[] vektor3 = new double[3];
      vektor3[0] = f3.getFunktionswert(random1);
      vektor3[1] = f3.getFunktionswert(random2);
      vektor3[2] = f3.getFunktionswert(random3);
      
      double determinante = calculateDeterminante(vektor1, vektor2, vektor3);
      
      durchlaeufe = i+1;
      
      if(Math.abs(determinante)>=Math.pow(10, -10))
        return true;        
    }
    return false;
  }
  
  public int getDurchlaeufe()
  {
    return durchlaeufe;
  }

  private double calculateDeterminante(double[] vektor1, double[] vektor2, double[] vektor3)
  {
    double[] kreuzProdukt = {vektor1[1] * vektor2[2] - vektor1[2] * vektor2[1],
                             vektor1[2] * vektor2[0] - vektor1[0] * vektor2[2],
                             vektor1[0] * vektor2[1] - vektor1[1] * vektor2[0]};

    return kreuzProdukt[0] * vektor3[0] +
           kreuzProdukt[1] * vektor3[1] +
           kreuzProdukt[2] * vektor3[2];
  }
}
